'use strict'

var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var CommonsChunkPlugin = require('./node_modules/webpack/lib/optimize/CommonsChunkPlugin')
var UglifyJsPlugin = require('./node_modules/webpack/lib/optimize/UglifyJsPlugin')

var releaseEnv = process.NODE_ENV || 'development'

var plugins = [
    new webpack.HotModuleReplacementPlugin(),
    new CommonsChunkPlugin({
        name: 'vendor',
        filename: 'vendor.js',
        minChunks: 2
    }),
    new ExtractTextPlugin({
        filename: 'bundle.css',
        disable: false,
        allChunks: true
    }),
    new UglifyJsPlugin({
        comments: false,
        compress: {
            warnings: false
        }
    })
]

plugins.push(new webpack.DefinePlugin({
    'process.env': {
        NODE_ENV: JSON.stringify(releaseEnv)
    }
}))

//default template
plugins.push(new HtmlWebpackPlugin({
    title: '碑林龙腾双创计划一览',
    env: releaseEnv,
    inject: true,
    chunks: ['vendor', 'app'],
    template: 'index.ejs',
    filename: 'index.html'
}))

module.exports = {
    entry: {
        app: './src/index.jsx',
        vendor: ['babel-polyfill', 'react', 'react-router', 'react-redux', 'whatwg-fetch', './src/lib/echarts.js']
        // bmap: './src/lib/bmap.min.js'
    },
    devServer: {
        contentBase: path.resolve(__dirname, './assets'),
        compress: true,
        port: 8082,
        host: '0.0.0.0',
        hot: true,
        inline: true,
        historyApiFallback: true
    },
    output: {
        path: path.resolve(__dirname, './assets/'),
        publicPath: '/',
        filename: 'js/[name].[hash:8].js',
        chunkFilename: 'js/[name].[hash:8].js'
    },
    // externals: {
    //     echars: 'echarts'
    // },
    resolve: {
        modules: [
            'src',
            'node_modules'
        ],
        extensions: ['.css', '.scss', '.js', '.jsx']
    },
    module: {
        rules: [{
            test: /\.html$/,
            use: [{
                loader: 'file-loader'
            }]
        }, {
            test: /\.(js|jsx)$/,
            exclude: [/node_modules/],
            use: [{
                loader: 'babel-loader',
                options: {
                    presets: ['react', 'es2015'],
                    plugins: ['transform-runtime', 'syntax-dynamic-import', 'transform-object-rest-spread']
                }
            }]
        }, {
            test: /\.scss$/,
            use: [
                'style-loader',
                'css-loader',
                'postcss-loader',
                'sass-loader'
            ]
        }, {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use: 'css-loader',
                publicPath: '/assets/css/'
            })
        }, {
            test: /\.(png|jpg|gif|ico|jpeg)$/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 1000,
                    name: 'images/[name].[hash].[ext]'
                }
            }]
        }, {
            test: /\.(eot|svg|ttf|woff|woff2)\??.*$/,
            loader: 'url-loader',
            options: {
                name: 'fonts/[name].[hash].[ext]'
            }
        }, {
            test: /\.json$/,
            loader: 'json-loader'
        }]
    },
    plugins: plugins
}